package com.cjj.common.entity;

import com.github.pagehelper.PageInfo;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Collections;
import java.util.List;

/**
 * @author DoyuTu
 */
@Data
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class PageResult<T> {

    private int page = Pageable.DEFAULT_PAGE;

    private int num = Pageable.DEFAULT_NUM;

    private long total;

    private int pages = 1;

    private boolean hasNext;

    private List<T> list;

    public static <T> PageResult<T> of(PageInfo<T> info) {
        return new PageResult<>(info.getPageNum(), info.getPageSize(), info.getTotal(), info.getPages(), info.isHasNextPage(), info.getList());
    }

    public static <T> PageResult<T> of(PageInfo<?> info, List<T> result) {
        if (info == null || result == null) {
            return new PageResult<>();
        }
        return new PageResult<>(info.getPageNum(), info.getPageSize(), info.getTotal(), info.getPages(), info.isHasNextPage(), result);
    }

    public static <T> PageResult<T> empty(Pageable pageable) {
        return new PageResult<>(pageable.getPage(), pageable.getNum(), 0, 0, false, Collections.emptyList());
    }

}
